# -*- coding: utf-8 -*-

import logging

from datetime import datetime, timedelta
from odoo import models, fields

_logger = logging.getLogger(__name__)


# 定时任务，车主未确认取消订单
class TaskVendorPassword(models.Model):
    _name = 'task.sale.order'

    name = fields.Char(string="Name")

    def cron_cancel_order(self):
        _logger.debug(u'定时任务启动，车主未确认取消订单...')

        cancel_time = self.env['tour.config.settings'].get_config('order_cancel_time', self.create_uid.id)
        deadline = datetime.now() - timedelta(minutes=int(cancel_time))

        orders = self.env['tour.sale.order'].search([('state', '=', 'draft')])
        for order in orders:
            if order.create_date > deadline:
                order.state = 'cancel'
